package com.kasa.gps.db.table;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.kasa.gps.db.OrderGPSDBAdapter;

public class LocationInfoTableAdapter extends OrderGPSDBAdapter {

	public static final String TABLE_NAME="locationinfo";
	
	//id
	public static final String ID="_id";
	//车辆计划id
	public static final String VEHICLESINFO_ID="vehiclesinfo_id";
	//经度
	public static final String LONGITUDE="longitude";
	//纬度
	public static final String LATITUDE="latitude";
	//国家地区省市县
	public static final String AREA="area";
	//街道
	public static final String STREE="stree";
	//更新时间
	public static final String UPDATETIME = "updatetime";
	//速度，暂时不入库
	public static final String SPEED = "speed";
	
	
	private Context context = null;
	
	private OrderGPSDBHelper dbHelper = null;
	private SQLiteDatabase sqlitedb = null;

	public LocationInfoTableAdapter(Context context){
		this.context = context;
	}
	
	private LocationInfoTableAdapter open()throws SQLException{
		this.dbHelper = new OrderGPSDBHelper(context);
		this.sqlitedb = this.dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		this.dbHelper.close();
		this.sqlitedb.close();
	}
	
	public boolean insertLocationInfo(int vehiclesinfo_id, double longitude,double latitude,String area,String stree,String updatetime){
		if(this.sqlitedb == null || !this.sqlitedb.isOpen()){
			open();
		}
		ContentValues values = new ContentValues();
		values.put(VEHICLESINFO_ID, vehiclesinfo_id);
		values.put(LONGITUDE, longitude);
		values.put(LATITUDE, latitude);
		values.put(AREA, area);
		values.put(STREE, stree);
		values.put(UPDATETIME, updatetime);
		
		return this.sqlitedb.insert(TABLE_NAME, null, values) != -1;
	}
	
	public boolean deleteLocationInfo(long vehiclesinfo_id){
		return this.sqlitedb.delete(TABLE_NAME, VEHICLESINFO_ID+"="+vehiclesinfo_id, null) > 0;
	}
}
